#!/bin/bash

# Ensure a valid display is set
if [ -z "$WAYLAND_DISPLAY" ]; then
    if [ -z "$DISPLAY" ]; then
        export DISPLAY=$(getLocalXDisplay)
    fi
fi

# Check if vulkaninfo exists
if [ ! -x "/usr/bin/vulkaninfo" ]; then
    echo "Vulkan not found"
    exit 1
fi

# Function to list Vulkan GPU devices
list_devices() {
    vulkaninfo | grep deviceName | awk -F'= ' '{print $2}'
}

# Function to count the number of Vulkan GPU devices
num_devices() {
    list_devices | wc -l
}

# Function to check if a discrete GPU exists
has_discrete() {
    vulkaninfo | grep -q PHYSICAL_DEVICE_TYPE_DISCRETE_GPU
    if [ $? -eq 0 ]; then
        # allow override
        radeon_prime=$(/usr/bin/batocera-settings-get -f /boot/batocera-boot.conf radeon-prime)
        if [ "$radeon_prime" = "false" ]; then
            echo "false"
        else
            echo "true"
        fi
    else
        echo "false"
    fi
}

# Function to retrieve the UUID of the discrete GPU
discrete_uuid() {
    device_type_output=$(vulkaninfo | grep deviceType)
    discrete_gpu_index=$(echo "$device_type_output" | grep -n PHYSICAL_DEVICE_TYPE_DISCRETE_GPU | cut -d: -f1)
    if [ -z "$discrete_gpu_index" ]; then
        echo "No discrete GPU found."
        exit 1
    fi

    device_uuid=$(vulkaninfo | grep deviceUUID | sed -n "${discrete_gpu_index}p" | awk -F'=' '{print $2}' | tr -d '[:space:]')
    if [ -z "$device_uuid" ]; then
        echo "Failed to retrieve UUID for the discrete GPU."
        exit 1
    fi

    echo "$device_uuid"
}

# Function to retrieve the name of the discrete GPU
discrete_name() {
    device_type_output=$(vulkaninfo | grep deviceType)
    discrete_gpu_index=$(echo "$device_type_output" | grep -n PHYSICAL_DEVICE_TYPE_DISCRETE_GPU | cut -d: -f1)
    if [ -z "$discrete_gpu_index" ]; then
        echo "No discrete GPU found."
        exit 1
    fi

    device_name=$(vulkaninfo | grep deviceName | sed -n "${discrete_gpu_index}p" | awk -F'= ' '{print $2}')
    if [ -z "$device_name" ]; then
        echo "Failed to retrieve name for the discrete GPU."
        exit 1
    fi

    echo "$device_name"
}

# Function to retrieve the name of the DEFAULT (first) GPU
default_name() {
    device_name=$(vulkaninfo | grep deviceName | sed -n '1p' | awk -F'= ' '{print $2}')
    
    if [ -z "$device_name" ]; then
        echo "Failed to retrieve name for the default GPU."
        exit 1
    fi

    echo "$device_name"
}

# Function to retrieve the index (GPU number) of the discrete GPU
discrete_index() {
    device_type_output=$(vulkaninfo | grep deviceType)
    discrete_gpu_index=$(echo "$device_type_output" | grep -n PHYSICAL_DEVICE_TYPE_DISCRETE_GPU | cut -d: -f1)
    # Subtract 1 from the index to get the Vulkan GPU number
    discrete_gpu_index=$((discrete_gpu_index - 1))
    if [ -z "$discrete_gpu_index" ]; then
        echo "No discrete GPU found."
        exit 1
    fi

    echo "$discrete_gpu_index"
}

# Function to check if Vulkan is available
has_vulkan() {
    vulkaninfo | grep -q deviceName
    if [ $? -eq 0 ]; then
        echo "true"
    else
        echo "false"
    fi
}

vulkan_version() {
    if [ "$(batocera-vulkan hasVulkan)" = "true" ]; then
        if [ "$(batocera-vulkan hasDiscrete)" = "true" ]; then
            discreteIndex=$(batocera-vulkan discreteIndex)
            apiVersion=$(vulkaninfo | grep -A8 "GPU$discreteIndex" | awk '/apiVersion/ {print $3}')
            echo $apiVersion
        else
            apiVersion=$(vulkaninfo | grep -A8 "GPU0" | awk '/apiVersion/ {print $3}')
            echo $apiVersion
        fi
    else
        echo "No Vulkan driver found!"
    fi
}

# Handle input options
case "$1" in
    "listDevices")
        list_devices
        ;;
    "numDevices")
        num_devices
        ;;
    "hasDiscrete")
        has_discrete
        ;;
    "discreteUUID")
        discrete_uuid
        ;;
    "discreteName")
        discrete_name
        ;;
    "discreteIndex")
        discrete_index
        ;;
    "defaultName")
        default_name
        ;;
    "hasVulkan")
        has_vulkan
        ;;
    "vulkanVersion")
        vulkan_version
        ;;
    *)
        echo "Invalid option. Usage: $0 {listDevices|numDevices|hasDiscrete|discreteUUID|discreteName|discreteIndex|defaultName|hasVulkan|vulkanVersion}"
        exit 1
        ;;
esac
